package com.yeyks.controller.finance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.dto.InvoiceInfoDTO;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.finance.FinanceInvoiceQueryParam;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.commonReference.service.other.InvoiceInfoService;
import com.yeyks.consumer.dal.domain.RechargeRecord;
import com.yeyks.consumer.service.RechargeRecordService;
import com.yeyks.order.dal.domain.OrderHead;
import com.yeyks.other.dal.domain.InvoiceInfo;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Api(tags = "V4.2.0 开票控制模块")
@RestController
@RequestMapping("/invoice")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FinanceInvoiceController {

    private final InvoiceInfoService invoiceInfoService;

    /**
     * 订单中的invoice_id
     */
    private final OrderHeadService orderHeadService;

    /**
     * VIP充值invoice_id
     */
    private final RechargeRecordService rechargeRecordService;

    @NeedToken
    @ApiOperation(value = "V4.2.0 分页查询 订单开票列表信息", response = InvoiceInfoDTO.class)
    @PostMapping("/c2b/list")
    public AjaxResponseBody orderInvoiceList(@Validated FinanceInvoiceQueryParam param){
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if(loginUser.getType() != AccountType.Administrator){
            throw new ServiceException("没有权限查看");
        }
        return AjaxResponseBody.success(invoiceInfoService.orderInvoiceQueryByParam(param));
    }

    @NeedToken
    @ApiOperation("V4.2.0 开票成功、 开票失败")
    @PostMapping("/c2b/edit/status")
    public AjaxResponseBody editInvoiceStatus(
            @ApiParam(name = "id", value = "开票ID")
            @RequestParam(value = "id") Integer id,
            @ApiParam(name = "status", value = "开票状态",allowableValues = "-1,1")
            @RequestParam(value = "status") Integer status){
        AjaxResponseBody body = AjaxResponseBody.failed();
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if(loginUser.getType() != AccountType.Administrator){
            throw new ServiceException("没有权限执行开票");
        }
        InvoiceInfo invoiceInfo = invoiceInfoService.getOne(new QueryWrapper<InvoiceInfo>().eq(InvoiceInfo.ID, id));
        if(null == invoiceInfo){
            throw new ServiceException("无效开票ID");
        }
        if(status != -1 && status != 1){
            throw new ServiceException("状态无效");
        }
        if(status == -1){
            //清空c2b订单的发票ID
            List<OrderHead> orderHeads = orderHeadService.list(new QueryWrapper<OrderHead>().eq(OrderHead.INVOICE_ID, id));
            if(!CollectionUtils.isEmpty(orderHeads)){
                orderHeads.forEach(orderHead -> {
                    orderHeadService.updateInvoiceIsNull(orderHead.getOrderNo());
                });
            }
        }
        invoiceInfo.setInvoiceStatus(status);
        boolean flag = invoiceInfoService.updateById(invoiceInfo);
        if(flag){
            body = AjaxResponseBody.success();
        }
        return body;
    }

    @NeedToken
    @ApiOperation("V4.2.0 分页查询 优享VIP充值开票列表")
    @PostMapping("/min/vip/list")
    public AjaxResponseBody rechargeInvoiceList(@Validated FinanceInvoiceQueryParam param){
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if(loginUser.getType() != AccountType.Administrator){
            throw new ServiceException("没有权限查看");
        }
        return AjaxResponseBody.success(invoiceInfoService.rechargeInvoiceList(param));
    }

    @NeedToken
    @ApiOperation("V4.2.0 开票成功、 开票失败")
    @PostMapping("/min/vip/edit/status")
    public AjaxResponseBody editRechargeInvoiceStatus(@ApiParam(name = "id", value = "开票ID")
                                                      @RequestParam(value = "id") Integer id,
                                                      @ApiParam(name = "status", value = "开票状态",allowableValues = "-1,1")
                                                      @RequestParam(value = "status") Integer status){
        AjaxResponseBody body = AjaxResponseBody.failed();
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if(loginUser.getType() != AccountType.Administrator){
            throw new ServiceException("没有权限执行开票");
        }
        InvoiceInfo invoiceInfo = invoiceInfoService.getOne(new QueryWrapper<InvoiceInfo>().eq(InvoiceInfo.ID, id));
        if(null == invoiceInfo){
            throw new ServiceException("无效开票ID");
        }
        if(status != -1 && status != 1){
            throw new ServiceException("状态无效");
        }
        if(status == -1){
            //清空充值开票的发票ID
            List<RechargeRecord> rechargeRecordList = rechargeRecordService.list(new QueryWrapper<RechargeRecord>().eq(RechargeRecord.INVOICE_ID, id
            ));
            if(!CollectionUtils.isEmpty(rechargeRecordList)){
                rechargeRecordList.forEach(rechargeRecord -> {
                    rechargeRecordService.updateInvoiceIsNull(rechargeRecord.getId());
                });
            }
        }
        invoiceInfo.setInvoiceStatus(status);
        boolean flag = invoiceInfoService.updateById(invoiceInfo);
        if(flag){
            body = AjaxResponseBody.success();
        }
        return body;
    }
}
